package com.heo.coder.packet;

import com.heo.coder.packet.base.BasePacket;
import com.heo.util.ByteHelper;
import com.heo.util.Utils;

public class RegisterPacketRsp extends BasePacket{

	public RegisterPacketRsp(String commandId, String reserve, String subcontract, String rsa, int bodyLength, String imei,
			String sequenceId, String body, String check) throws Exception {
		super(commandId, reserve, subcontract, rsa, bodyLength, imei, sequenceId, body, check);
	}
	
	/**
	 * 查询终端属性
	 */
	public void setRegisterResult(String sequenceIds, int result, String suthe) {
		StringBuffer contBody = new StringBuffer();
		byte[] b2 = null;
		if(result == 0) {
			b2 = new byte[] {(byte)0x0};
		}else if(result == 1) {
			b2 = new byte[] {(byte)0x1};
		}else if(result == 2) {
			b2 = new byte[] {(byte)0x2};
		}else if(result == 3) {
			b2 = new byte[] {(byte)0x3};
		}else if(result == 4) {
			b2 = new byte[] {(byte)0x4};
		}
		contBody.append(sequenceIds);
		contBody.append(ByteHelper.byteToHexStringNoSpace(b2));
		if(Utils.checkNull(suthe)) {
			suthe = "1234";
		}
		contBody.append(ByteHelper.strTo16(suthe));
		body = contBody.toString();
		byte[] bodyByte = ByteHelper.hexStringToBytes(body);
		int bodyInt = bodyByte.length;
		String binary = reserve + subcontract + rsa + ByteHelper.hex10To2String(bodyInt, 10);
		String hex = ByteHelper.binaryString2hexString(binary);
		setHead(hex);
		setBodyLength(bodyInt);
		
		String hexCheck = getCommandId() + head + imei + sequenceId + body;
    	byte[] hexCheckBytes = ByteHelper.hexStringToBytes(hexCheck);
		byte xorByte = ByteHelper.getXor(hexCheckBytes);
		String checkCodeStr = ByteHelper.byteToHexString(new byte[] {xorByte});
		check = checkCodeStr;
	}
	 
	@Override
	public String toString() {
		return "SocketPacket: [" + commandId + "] [" + START_MARKER_LENGTH_CODE + getCommandId() + getHead() + getImei() + getSequenceId() + getBody() + getCheck() + END_MARKER_LENGTH_CODE + "]";
	}

	@Override
	public String getPacketBody() {
		return super.body;
	}

	
	
}
